package net.lahuobao.mall.controller.admin;

import com.alibaba.fastjson.JSONObject;
import net.lahuobao.enums.EOSSFolderName;
import net.lahuobao.mall.entity.admin.UeditorUploadResult;
import net.lahuobao.util.OSSFileOSUtil;
import org.jeecgframework.core.common.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by clark on 2016/8/28 0028.
 * ueditor ue上传controller
 *
 */
@Controller
@RequestMapping("/ueditorUploadController")
public class UeditorUploadController extends BaseController{

    /**
     * 图片上传
     * @param upfile
     * @param response
     */
    @RequestMapping(params = "imageup")
    @ResponseBody
    public void upfile(@RequestParam("upfile") MultipartFile upfile, HttpServletResponse response) {
        try {
            System.out.println(upfile);
            //处理上传
            String url = uploadImage(upfile, EOSSFolderName.UEDITOR_ARTICLE_PATH);
            //处理返回信息
            UeditorUploadResult uploadResult = new UeditorUploadResult();
            uploadResult.setState("SUCCESS");
            uploadResult.setOriginal(upfile.getOriginalFilename());
            uploadResult.setType(upfile.getContentType());
            uploadResult.setSize(upfile.getSize());
            uploadResult.setUrl(url);
            uploadResult.setTitle(upfile.getOriginalFilename());
            //写回数据
            renderData(response, JSONObject.toJSONString(uploadResult));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
